package asia.kalin.controller;

import asia.kalin.domain.ao.Result;
import asia.kalin.domain.po.UserInfo;
import asia.kalin.mapper.UserInfoMapper;
import asia.kalin.utils.SendEmailUtil;
import asia.kalin.utils.ThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.mail.EmailException;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: Kalin
 * @Name：FeedbackController
 * @Date: 2025/3/25
 * @description
 */

@RestController
@RequestMapping("/userFeedback")
@RequiredArgsConstructor
public class FeedbackController {

    private final UserInfoMapper userInfoMapper;

    @PostMapping("/sendEmail")
    public Result sendUserFeedback(@RequestParam String text){
        // 获取到当前用户id
        Map<String,Object> claims = ThreadLocalUtil.get();
        String userId = (String) claims.get("userId");

        // 查询到用户名信息
        UserInfo userInfo = userInfoMapper.selectById(userId);

        // 发送邮件
        try {
            SendEmailUtil.sendQQEmail(text,userInfo.getUsername());
        } catch (EmailException e) {
            return new Result<>(1,"发送失败",e.getMessage());
        }

        return new Result<>(0,"发送成功",null);
    }
}
